<?php
/*
* File：生成卡密
* Author：易如意
* QQ：51154393
* Url：www.eruyi.cn
*/

require_once 'globals.php';

$type = isset($_POST['type']) ? addslashes($_POST['type']) : 'TK';
$num = isset($_POST['num']) ? intval($_POST['num']) : 1;
$out = isset($_POST['out']) ? intval($_POST['out']) : 0;
$submit = isset($_POST['submit']) ? addslashes($_POST['submit']) : '';
if($submit){
	$str = '';
	for($i=1;$i<=$num;$i++){
		$key=getcode();
		$sql="INSERT INTO `eruyi_kami`(`kami`, `type`) VALUES ('$key','$type')";
		$query=$db->query($sql);
		$str .= $key . "\r\n";
	}
	if($out == 1){
		header("Content-Type: application/octet-stream");
		header("Content-Disposition: attachment; filename={$type}_kami.txt");
		echo "==============卡密开始================\r\n\r\n";
		echo $str;
		echo "\r\n\r\n==============卡密结束================";
		exit;
	}else{
		include_once 'header.php';
		echo "<script>alert('生成成功');</script>";
	}
}else{
	include_once 'header.php';
}

function getcode(){ 
	$str = null;  
	$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";  
	$max = strlen($strPol)-1;  
	for($i=0;$i<32;$i++){
		$str.=$strPol[rand(0,$max)];
	}  
	return $str; 
}
?>
<form action="" method="post" id="addimg" name="addimg">
<div id="post">
<div>
    <label for="type" id="type_label">卡密类型：</label>
    <select name="type" id="type">
	  <option value="TK">天卡</option>
	  <option value="ZK">周卡</option>
	  <option value="YK">月卡</option>
	  <option value="BNK">半年卡</option>
	  <option value="NK">年卡</option>
	  <option value="YJK">永久卡</option>
	</select>
	
    <label for="num" id="num_label">生成个数：</label>
    <input type="text" maxlength="3" name="num" id="num" style="width:30px;" value="1"/>

	<input type="submit" name="submit" value="生成卡密" class="button" />
	
	<input type="checkbox" name="out" id="out" value="1"/>
	<label for="out" id="out_label">生成卡密后直接导出</label>
</div>
</div>
</form>
<script> 
var div = document.getElementById('add_kami'); 
div.setAttribute("class", "show"); 
</script> 
<?php 
include_once 'footer.php';
?>